home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7525 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.1 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What is &Variable (declared as: char Variable[10])?
  5. Date: 26 Feb 1996 03:46:14 GMT
  6. Organization: Los Alamos National Laboratory
  7. Distribution: world
  8. Message-ID: <TANMOY.96Feb25204614@qcd.lanl.gov>
  9. References: <4gqpa1$3h9@alcor.usc.edu>
  10. NNTP-Posting-Host: qcd.lanl.gov
  11. Mime-Version: 1.0
  12. Content-Type: text
  13. In-reply-to: wawda@alcor.usc.edu's message of 25 Feb 1996 14:53:53 -0800
  14.  
  15. In article <4gqpa1$3h9@alcor.usc.edu>
  16. wawda@alcor.usc.edu (Abu Wawda) writes:
  17.  
  18. AW: I'm having trouble understanding what the address of a static array
  19. AW: is. For example, if I declare a variable called myarray as:
  20. AW:     char myarray[10];
  21. AW: then what could &myarray possibly mean? myarray is not a pointer, so
  22. AW: &myarray could not possibly be the address of the variable myarray
  23. AW: (like it would be if I did char* myarray and then asked for &myarray).
  24.  
  25. If you wrote int i, what does &i mean? Will you argue that it could
  26. not possibly be a pointer to the variable i because i is not apointer
  27. :-)? 
  28.  
  29. &myarray is a pointer to the array myarray. It is likely to have the
  30. same `value' as &myarray[0], but arithmetic on it works
  31. differently. Thus &myarray[0]+1 would skip over one char (i.e. it
  32. would be &myarray[1]), but &myarray+1 would skip over the entire array
  33. and hence point just beyond the entire array.
  34.  
  35. It has absolutely nothing to do with char**.
  36.  
  37. AW: 
  38. AW: Functions such as scanf() allow the following:
  39. AW: 
  40. AW:     char myarray[10];
  41. AW: 
  42. AW:     scanf("%s",&myarray);
  43. AW: 
  44. AW: but I don't understand what scanf() could possibly be taking in the
  45.  
  46. `allow' is a dangerous word. %s must be given a char*, not a
  47. char(*)[10] as you have done. This causes undefined behaviour, and the
  48. compiler can do anything it likes, including making it seem like it
  49. works. 
  50.  
  51. (By the way, the standard has a garbled wording either in the scanf
  52. or the printf description of %s, I forget which. I am ignoring that.)
  53.  
  54. AW: second parameter. It can't be: char** since myarray is not a
  55. AW: pointer. I CAN understand how the following would work:
  56. AW: 
  57. AW:     char* myarray;
  58. AW: 
  59. AW:     myarray = (char*) malloc(10);
  60. AW:     scanf("%s",&myarray);
  61. AW: 
  62. AW: Then scanf() is simply taking a char** (pointer to a character
  63. AW: pointer, or in other words the address of the pointer myarray) in its
  64.  
  65. And again, that leads to undefined behaviour.
  66.  
  67. AW: second paremeter. However, if I write my own function like this:
  68. AW: 
  69. AW:     void func(char** p)
  70. AW:     {
  71. AW:         // do something here
  72. AW:     }    
  73. AW: 
  74. AW: I cannot pass &myarray if I declare it as: char myarray[10]. So how do
  75. AW: this work? Thanks in advance,
  76.  
  77. I do not know what you mean to ask in this question.
  78.  
  79. Cheers
  80. Tanmoy
  81. --
  82. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  83. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  84. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  85. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  86. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  87. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  88.